home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / c / agl103p.lha / src / gltest / gltest.c next >
Encoding:
C/C++ Source or Header  |  1994-12-09  |  9.6 KB  |  635 lines

  1. #include<exec/types.h>
  2. #include<intuition/intuitionbase.h>
  3. #include<stdio.h>
  4. #include<stdlib.h>
  5. #include<string.h>
  6. #include<math.h>
  7.  
  8. #ifdef __SASC
  9. #include<functions.h>
  10. #endif
  11.  
  12. #ifdef AZTEC_C
  13. #include<functions.h>
  14. #endif
  15.  
  16. #ifdef LATTICE
  17. #include<lattice_amiga.h>
  18. #define SEEK_CUR 1
  19. #endif
  20.  
  21. #include<gl.h>
  22. #include<device.h>
  23.  
  24. #define PIE    3.14159265
  25. #define DEG    (PIE/180.0)
  26.  
  27. #define MOVETEST    FALSE
  28. #define REDRAWTEST    FALSE
  29.  
  30. #include"prototypes.h"
  31.  
  32. short CubeVert[8][3]=
  33.     {
  34.     -1,-1,-1,
  35.     -1,-1, 1,
  36.     -1, 1, 1,
  37.     -1, 1,-1,
  38.      1,-1,-1,
  39.      1,-1, 1,
  40.      1, 1, 1,
  41.      1, 1,-1,
  42.     };
  43.  
  44. short CubeEdge[16]=
  45.     {
  46.     1,2,3,0,
  47.     3,7,6,2,
  48.     6,5,4,7,
  49.     4,0,1,5,
  50.     };
  51.  
  52. long Focus,Wid[10],MaxX,MaxY;
  53.  
  54. short EventDebug=FALSE;
  55. short DoubleBuffer=TRUE;
  56. short Window0=FALSE;
  57. short Window1=FALSE;
  58. short Window2=FALSE;
  59.  
  60.  
  61. /******************************************************************************
  62. int    main(int argc,char **argv)
  63.  
  64. ******************************************************************************/
  65. /*PROTOTYPE*/
  66. int main(int argc,char **argv)
  67.     {
  68.     long mx,my;
  69.     long frame=0;
  70.  
  71.     short quit=0;
  72.     short signal;
  73.     short data;
  74.     short hires=FALSE;
  75.     short c,m;
  76.  
  77.     printf("gltest: little test program for Amiga GL by Jason Weber\n");
  78.     printf("\nArguments: s=singlebuffer  h=hires  1,2=toggle which windows to run  e=print events\n");
  79.     printf("\nTo kill, hit ESC or button in upper left of window\n\n");
  80.  
  81.     if(argc>1)
  82.         {
  83.         for(m=0;m<strlen(argv[1]);m++)
  84.             switch(c=argv[1][m])
  85.                 {
  86.                 case 'e':
  87.                     EventDebug=TRUE;
  88.                     break;
  89.                 case 's':
  90.                     DoubleBuffer=FALSE;
  91.                     break;
  92.                 case 'h':
  93.                     hires=TRUE;
  94.                     break;
  95.                 case '1':
  96.                     Window0^=TRUE;
  97.                     break;
  98.                 case '2':
  99.                     Window1^=TRUE;
  100.                     break;
  101. #if TRUE
  102.                 case '3':
  103.                     Window2^=TRUE;
  104.                     break;
  105. #endif
  106.                 }
  107.         }
  108.  
  109.     /* force at least 1 window open */
  110.     if(!Window1 && !Window2)
  111.         Window0=TRUE;
  112.  
  113. #if _AMIGA
  114.  
  115.     /* optionally use lores, (hires is default) */
  116.     if(!hires)
  117.         AGLconfig(350,225,4);
  118.  
  119. #endif
  120.  
  121.     MaxX=getgdesc(GD_XPMAX);
  122.     MaxY=getgdesc(GD_YPMAX);
  123.     Focus=0;
  124.  
  125.     foreground();
  126.  
  127.     if(Window0)
  128.         window0(frame,TRUE);
  129.  
  130.     if(Window1)
  131.         window1(frame,TRUE);
  132.  
  133.     if(Window2)
  134.         window2(frame,TRUE);
  135.  
  136.     mapcolor(8,112,112,112);
  137.  
  138.     qdevice(WINQUIT);
  139.     qdevice(ESCKEY);
  140.     qdevice(KEYBD);
  141.     qdevice(F5KEY);
  142.     qdevice(TWOKEY);
  143.     qdevice(SEVENKEY);
  144.     qdevice(LEFTMOUSE);
  145.     qdevice(MIDDLEMOUSE);
  146.     qdevice(RIGHTMOUSE);
  147.  
  148.     tie(MIDDLEMOUSE,MOUSEX,MOUSEY);
  149.     tie(SEVENKEY,MOUSEY,0);
  150.  
  151.     while(!quit)
  152.         {
  153.         while(qtest())
  154.             {
  155.             signal=qread(&data);
  156.  
  157.             switch(signal)
  158.                 {
  159.                 case REDRAW:
  160.                     if(EventDebug)
  161.                         printf("REDRAW      %d\n",data);
  162.  
  163. #if REDRAWTEST
  164.                     if(Window2 && data==Wid[2])
  165.                         window2(frame,FALSE);
  166. #endif
  167.  
  168.                     break;
  169.  
  170.                 case INPUTCHANGE:
  171.                     if(EventDebug)
  172.                         printf("INPUTCHANGE %d\n",data);
  173.                     Focus=data;
  174.                     break;
  175.  
  176.                 case KEYBD:
  177.                     if(EventDebug)
  178.                         printf("KEYBD       %3d '%c'\n",data,data);
  179.                     break;
  180.  
  181.                 case F5KEY:
  182.                     if(EventDebug)
  183.                         printf("F5KEY       %3d\n",data);
  184.                     break;
  185.  
  186.                 case TWOKEY:
  187.                     if(EventDebug)
  188.                         printf("TWOKEY      %3d\n",data);
  189.                     break;
  190.  
  191.                 case SEVENKEY:
  192.                     if(EventDebug)
  193.                         printf("SEVENKEY    %3d\n",data);
  194.                     break;
  195.  
  196.                 case MOUSEX:
  197.                     if(EventDebug)
  198.                         printf("MOUSEX      %d\n",data);
  199.                     break;
  200.  
  201.                 case MOUSEY:
  202.                     if(EventDebug)
  203.                         printf("MOUSEY      %d\n",data);
  204.                     break;
  205.  
  206.                 case LEFTMOUSE:
  207.                     if(EventDebug)
  208.                         printf("LEFTMOUSE   %d\n",data);
  209.                     break;
  210.  
  211.                 case MIDDLEMOUSE:
  212.                     if(EventDebug)
  213.                         printf("MIDDLEMOUSE %d\n",data);
  214.                     break;
  215.  
  216.                 case RIGHTMOUSE:
  217.                     if(EventDebug)
  218.                         printf("RIGHTMOUSE  %d\n",data);
  219.                     break;
  220.  
  221.                 case WINQUIT:
  222.                     if(EventDebug)
  223.                         printf("WINQUIT     %d\n",data);
  224.  
  225.                 case ESCKEY:
  226.                     quit=1;
  227.                     break;
  228.  
  229.                 default:
  230.                     printf("Undefined queue: signal=%d data=%d\n",signal,data);
  231.                     break;
  232.                 }
  233.             }
  234.  
  235.         if(Window0)
  236.             window0(frame,FALSE);
  237.  
  238.         if(Window1)
  239.             window1(frame,FALSE);
  240.  
  241. #if !REDRAWTEST
  242.  
  243.         if(Window2)
  244.             window2(frame,FALSE);
  245.  
  246. #endif
  247.  
  248.         frame++;
  249.         }
  250.  
  251.     if(Window0)
  252.         winclose(Wid[0]);
  253.     if(Window1)
  254.         winclose(Wid[1]);
  255.     if(Window2)
  256.         winclose(Wid[2]);
  257.  
  258.     return 0;
  259.     }
  260.  
  261.  
  262. /******************************************************************************
  263. void    drawcube(short fill)
  264.  
  265. ******************************************************************************/
  266. /*PROTOTYPE*/
  267. void drawcube(short fill)
  268.     {
  269.     static char string[2];
  270.     short i,j;
  271.  
  272.     if(fill)
  273.         {
  274.         for(j=0;j<4;j++)
  275.             {
  276.             bgnpolygon();
  277.  
  278.             for(i=0;i<4;i++)
  279.                 v3s(CubeVert[CubeEdge[j*4+i]]);
  280.  
  281.             endpolygon();
  282.             }
  283.         }
  284.     else
  285.         {
  286.         bgnline();
  287.  
  288.         for(i=0;i<16;i++)
  289.             v3s(CubeVert[CubeEdge[i]]);
  290.  
  291.         endline();
  292.         }
  293.     }
  294.  
  295.  
  296. /******************************************************************************
  297. void    window0(long frame,long init)
  298.  
  299.     Spinning Cube
  300.  
  301. ******************************************************************************/
  302. /*PROTOTYPE*/
  303. void window0(long frame,long init)
  304.     {
  305.     char string[100];
  306.     long x,y,size;
  307.     long mx,my;
  308.     long wx,wy;
  309.     float angle;
  310.  
  311.     if(init)
  312.         {
  313.         x=MaxX/8;
  314.         y=MaxY/10;
  315.         size=MaxY/4;
  316.  
  317. /*         noborder(); */
  318. /*         prefposition(x,x+size*3/2,y,y+size); */
  319.         prefsize(size*3/2,size);
  320.  
  321.         Wid[0]=winopen("Spin Cube");
  322.         if(DoubleBuffer)
  323.             doublebuffer();
  324.         gconfig();
  325.         }
  326.     else
  327.         {
  328.         winset(Wid[0]);
  329.  
  330. #if MOVETEST
  331.  
  332.         while(getbutton(MIDDLEMOUSE))
  333.             {
  334.             mx=getvaluator(MOUSEX);
  335.             my=getvaluator(MOUSEY);
  336.  
  337.             winmove(mx-20,my-20);
  338.             }
  339.  
  340.  
  341.         mx=getvaluator(MOUSEX)-20;
  342.         my=getvaluator(MOUSEY)-20;
  343.  
  344.         if(getbutton(RIGHTMOUSE))
  345.             winposition(mx,mx+20+frame%20,my,my+20+(frame+15)%30);
  346.  
  347. #endif
  348.  
  349.  
  350.         angle=frame*2.0;
  351.  
  352.         if(frame%50 == 0)
  353.             {
  354.             sprintf(string,"Spin Cube %d",frame);
  355.             wintitle(string);
  356.             }
  357.  
  358.         getsize(&wx,&wy);
  359.         viewport(0,wx-1,0,wy-1);
  360.         perspective(600,wx/(float)wy,0.1,10.0);
  361.  
  362.         pushmatrix();
  363.  
  364.         translate(0.0,0.0,5.0);
  365.         rot(angle,'x');
  366.         rot(angle,'z');
  367.  
  368.         color(BLUE);
  369.         clear();
  370.  
  371.         color(YELLOW);
  372.         drawcube(FALSE);
  373.  
  374.         popmatrix();
  375.         if(DoubleBuffer)
  376.             swapbuffers();
  377.         }
  378.     }
  379.  
  380.  
  381. /******************************************************************************
  382. void    window1(long frame,long init)
  383.  
  384.     Moving Scene
  385.  
  386. ******************************************************************************/
  387. /*PROTOTYPE*/
  388. void window1(long frame,long init)
  389.     {
  390.     static float stator1[4][2]=
  391.         {
  392.         -1.0,    -1.0,
  393.         1.0,    -1.0,
  394.         1.0,    1.0,
  395.         -1.0,    1.0,
  396.         };
  397.     static float stator2[8][2];
  398.     static float rotor[5][2]=
  399.         {
  400.         0.0,    -0.8,
  401.         0.4,    -0.4,
  402.         -0.4,    0.4,
  403.         0.0,    0.8,
  404.         };
  405.     static float craft[5][2]=
  406.         {
  407.         -0.3,    -1.0,
  408.         -0.5,    0.0,
  409.         0.0,    1.0,
  410.         0.5,    0.0,
  411.         0.3,    -1.0,
  412.         };
  413.  
  414.     char string[100];
  415.  
  416.     long wx,wy,mx,my,v;
  417.     long x,y,size;
  418.  
  419.     float angle;
  420.  
  421.  
  422.     if(init)
  423.         {
  424.         noborder();
  425.  
  426.         x=MaxX/2;
  427.         y=MaxY/3;
  428.         size=MaxX/3;
  429.  
  430.         prefposition(x,x+size,y,y+size);
  431.         Wid[1]=winopen("Scene");
  432.  
  433.         if(DoubleBuffer)
  434.             doublebuffer();
  435.         gconfig();
  436.  
  437.         for(v=0;v<8;v++)
  438.             {
  439.             angle=v*45*DEG;
  440.             stator2[v][0]=0.8*sin(angle);
  441.             stator2[v][1]=0.8*cos(angle);
  442.             }
  443.         }
  444.     else
  445.         {
  446.         winset(Wid[1]);
  447.         ortho2(-10.0,10.0,-10.0,10.0);
  448.  
  449.         angle=frame*3.0;
  450.  
  451.         color(8);
  452.         clear();
  453.  
  454.         pushmatrix();
  455.         pushmatrix();
  456.  
  457.         translate(0.0,8.5,0.0);
  458.  
  459.         color(WHITE);
  460.  
  461.         cmovs((short)-8,(short)0,(short)0);
  462.         if(getbutton(LEFTMOUSE))
  463.             charstr("Left");
  464.  
  465.         cmov2s((short)-2,(short)0);
  466.         if(getbutton(MIDDLEMOUSE))
  467.             charstr("Middle");
  468.  
  469.         cmov(4.0,0.0,0.0);
  470.         if(getbutton(RIGHTMOUSE))
  471.             charstr("Right");
  472.  
  473.         translate(0.0,-1.5,0.0);
  474.  
  475.         cmovs((short)-8,(short)0,(short)0);
  476.         if(getbutton(TWOKEY))
  477.             charstr("Two");
  478.  
  479.         cmov2s((short)-2,(short)0);
  480.         if(getbutton(SEVENKEY))
  481.             charstr("Seven");
  482.  
  483.         cmov2s((short)4,(short)0);
  484.         if(getbutton(F5KEY))
  485.             charstr("F5");
  486.  
  487.         translate(0.0,-1.5,0.0);
  488.  
  489.         getorigin(&wx,&wy);
  490.         mx=getvaluator(MOUSEX);
  491.         my=getvaluator(MOUSEY);
  492.         sprintf(string,"%3d,%3d %3d,%3d",mx,my,mx-wx,my-wy);
  493.         cmov2(-8.0,0.0);
  494.         charstr(string);
  495.  
  496.         translate(0.0,-1.5,0.0);
  497.         sprintf(string,"Focus %d",Focus);
  498.         cmov2(0.0,0.0);
  499.         charstr(string);
  500.  
  501.         popmatrix();
  502.  
  503.         pushmatrix();
  504.  
  505.         translate(-5.0,3.0,0.0);
  506.         scale(1.5,1.5,1.5);
  507.  
  508.         color(11);
  509.         bgnpolygon();
  510.         for(v=0;v<4;v++)
  511.             v2f(stator1[v]);
  512.         endpolygon();
  513.  
  514.         color(15);
  515.         bgnpolygon();
  516.         for(v=0;v<8;v++)
  517.             v2f(stator2[v]);
  518.         endpolygon();
  519.  
  520.         color(GREEN);
  521.         rot(-4.0*angle,'z');
  522.         bgnpolygon();
  523.         for(v=0;v<4;v++)
  524.             v2f(rotor[v]);
  525.         endpolygon();
  526.  
  527.         popmatrix();
  528.  
  529.         translate(2.0,-3.0,0.0);
  530.         rot(angle,'z');
  531.         translate(5.0,0.0,0.0);
  532.  
  533.         color(RED);
  534.         bgnpolygon();
  535.         for(v=0;v<5;v++)
  536.             v2f(craft[v]);
  537.         endpolygon();
  538.  
  539.         pushmatrix();
  540.  
  541.         scale(1.0+0.5*sin(frame/2.0),0.75+0.25*cos(frame/2.0),0.0);
  542.         color(GREEN);
  543.         bgnpolygon();
  544.         for(v=0;v<5;v+=2)
  545.             v2f(craft[v]);
  546.         endpolygon();
  547.  
  548.         popmatrix();
  549.  
  550.         color(BLUE);
  551.         cmov2(0.0,0.0);
  552.         charstr("  Scout");
  553.  
  554.         popmatrix();
  555.  
  556.         if(DoubleBuffer)
  557.             swapbuffers();
  558.         }
  559.     }
  560.  
  561.  
  562.  
  563. /******************************************************************************
  564. void    window2(long frame,long init)
  565.  
  566.     Shaded Plane
  567.  
  568. ******************************************************************************/
  569. /*PROTOTYPE*/
  570. void window2(long frame,long init)
  571.     {
  572.     static float backcolor[3]={0.0,1.0,1.0};
  573.     static float forecolor[3]={1.0,1.0,0.0};
  574.  
  575.     float specific_color[3];
  576.  
  577.     char string[100];
  578.  
  579.     long x,y,size;
  580.     long wx,wy;
  581.  
  582.     float angle;
  583.     float scale;
  584.  
  585.     if(init)
  586.         {
  587.         x=MaxX/10;
  588.         y=MaxY/2;
  589.         size=MaxY/3;
  590.  
  591.         prefposition(x,x+size*3/2,y,y+size);
  592. /*         prefsize(size*3/2,size); */
  593.         Wid[2]=winopen("Filled Box");
  594.  
  595.         if(DoubleBuffer)
  596.             doublebuffer();
  597.  
  598.         RGBmode();
  599.  
  600.         gconfig();
  601.         }
  602.     else
  603.         {
  604.         winset(Wid[2]);
  605.  
  606.         angle=frame*2.0;
  607.  
  608.         getsize(&wx,&wy);
  609.         viewport(0,wx-1,0,wy-1);
  610.         perspective(600,wx/(float)wy,0.1,10.0);
  611.  
  612.         pushmatrix();
  613.  
  614.         translate(0.0,0.0,5.0);
  615.         rot(angle,'x');
  616.         rot(angle,'z');
  617.  
  618.         scale=0.5+0.5*sin(angle*DEG);
  619.         for(x=0;x<3;x++)
  620.             specific_color[x]=scale*backcolor[x];
  621.         c3f(specific_color);
  622.         clear();
  623.  
  624.         scale=0.5+0.5*cos(angle*DEG);
  625.         for(x=0;x<3;x++)
  626.             specific_color[x]=scale*forecolor[x];
  627.         c3f(specific_color);
  628.         drawcube(TRUE);
  629.  
  630.         popmatrix();
  631.         if(DoubleBuffer)
  632.             swapbuffers();
  633.         }
  634.     }
  635.